package com.lening.controller;

import com.lening.pojo.TbTypeTemplate;
import com.lening.sellergoods.service.TypeTemplateService;
import entity.PageResult;
import entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("typeTemplate")
public class TypeTemplateController {

    @Resource
    private TypeTemplateService typeTemplateService;
    //全查
    @RequestMapping("findAll")
    public List<TbTypeTemplate> findAll(){
        List<TbTypeTemplate> all = typeTemplateService.findAll();
        return all;
    }
    //分页
    @RequestMapping("findPage")
    public PageResult findPage(Integer page, Integer rows){
        PageResult page1 = typeTemplateService.findPage(page, rows);
        return page1;
    }

    /**
     * 分页条件查询
     * @param page
     * @param rows
     * @param searchEntity
     * @return
     */
    @RequestMapping("search")
    public PageResult search(Integer page, Integer rows, @RequestBody TbTypeTemplate searchEntity){
        PageResult page1 = typeTemplateService.findPage(searchEntity, page, rows);
        return page1;
    }

    @RequestMapping("findOne")
    public TbTypeTemplate findOne(Long id){
        TbTypeTemplate one = typeTemplateService.findOne(id);
        return one;
    }
    @RequestMapping("add")
    public Result add(@RequestBody TbTypeTemplate entity){
        int res = 0;
        try {
            typeTemplateService.add(entity);
            res = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res>0?new Result(true,"添加成功"):new Result(false,"添加失败");
    }
    @RequestMapping("update")
    public Result update(@RequestBody TbTypeTemplate entity){
        int res = 0;
        try {
            typeTemplateService.update(entity);
            res = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res>0?new Result(true,"修改成功"):new Result(false,"修改失败");
    }
    @RequestMapping("delete")
    public Result delete(@RequestBody Long[] ids){
        int res = 0;
        try {
            typeTemplateService.delete(ids);
            res = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res>0?new Result(true,"删除成功"):new Result(false,"删除失败");
    }

    @RequestMapping("seletOptionList")
    public List<Map> seletOptionList(){
        return typeTemplateService.selectOptionList();
    }


    @RequestMapping("findOneOption")
    public Map findOneOption(Long tid){
        Map map =  typeTemplateService.findOneOption(tid);
        return  map;
    }


}
